/*
 * @Description: 代码定义上下文
 * @Author: Rfan
 * @Date: 2022-01-08 16:30:05
 * @LastEditTime: 2022-02-15 11:13:34
 */

import { createContext, useContext, useEffect, useReducer } from 'react';
import { addFond } from '@/api/system';
import { CodeDefReducer } from '../reducers/CodeDefReducer';
import { ADD_FONT } from '../constants';

const initialState = {
  // 列表相关
  listQuery: {
    pageNo: 1,
    pageSize: 10,
    param: {
      comment: '',
      fondCode: '',
      fondName: ''
    }
  },
  loading: false,
  list: [
    { id: 1, name: 'a' },
    { id: 2, name: 'b' },
    { id: 3, name: 'c' }
  ]
};

const CodeDefContext = createContext();

const CodeDefContextProvider = (props) => {
  const [state, dispatch] = useReducer(CodeDefReducer, initialState);

  const actions = {
    /**
     * @description: 打开新增代码定义弹窗
     */

    onAddFond(data) {
      addFond(data).then((res) => {
        dispatch({ type: ADD_FONT, data: res.data });
      });
    }
  };

  // watch选中的单位来加载代码定义
  useEffect(() => {}, []);

  return (
    <CodeDefContext.Provider value={{ ...state, ...actions }}>
      {props.children}
    </CodeDefContext.Provider>
  );
};

export const useCodeDefContext = () => {
  return useContext(CodeDefContext);
};

export { CodeDefContext, CodeDefContextProvider };
